//
//  ProjectManagementViewController.m
//  MagicZhao
//
//  Created by hy002 on 2017/12/15.
//  Copyright © 2017年 HUAYU. All rights reserved.
//

#import "ProjectManagementViewController.h"
#import "DailyViewController.h"
#import "DailyDetailsViewController.h"
#import "Helper.h"
#import "TimeModel.h"
#import "projectModel.h"
#import "werkerModel.h"
#define componentCount 2
#define majorComponent 0
#define gradeComponent 1
#define majorComponentWidth 165
#define gradeComponentWidth 70

static NSString * CellIdentifier = @"homeCell";
static NSString * CellPath = @"dailyCell";
static NSString * CellNamePath = @"nameCell";
@interface ProjectManagementViewController ()<UIScrollViewDelegate,UICollectionViewDelegate,UICollectionViewDataSource,UITableViewDelegate,UITableViewDataSource, UIPickerViewDelegate, UIPickerViewDataSource>
{
    UIView *leftView;
    UIPickerView *selectPickerView;
    NSArray *majorNames;
    NSArray *grades;
    NSInteger        selectedMajor;
    NSInteger        selectedGrade;
}
@property (nonatomic, strong)UIScrollView *scrollView;
@property (nonatomic, strong)UITableView *tableview;
@property (nonatomic, strong)UICollectionView *collectionView;
@property (nonatomic, strong)UICollectionView *nameCollectionView;
@property (nonatomic, strong)UIView *theLineView;
@property (nonatomic, strong)NSArray *nameAry;
@property (nonatomic, strong)NSMutableArray *dayAry;
@property (nonatomic, assign)NSInteger closeType;
@property (nonatomic, assign)NSInteger page;
@property (nonatomic, strong)NSMutableArray *dailyAry;
@property (nonatomic, strong)NSArray *allWorkerArray;
@property (nonatomic, strong)NSMutableArray *allCloseAry;
@property (nonatomic, assign)BOOL isQX;
@property (nonatomic, strong) UIView *coverView;
@property (nonatomic, strong) UIView *tipView;

@end

@implementation ProjectManagementViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    _page = 0;
    [self requestWorkerDailyList];

}

- (void)viewDidLoad {
    [super viewDidLoad];
    majorNames=[[NSArray alloc]initWithObjects:@"8", @"7", @"6", @"5",@"4",@"3",@"2",@"1",@"0",nil ];
    grades=[[NSArray alloc]initWithObjects:@"5",@"0",nil];
    _dayAry = [NSMutableArray arrayWithCapacity:0];
    _dailyAry = [NSMutableArray arrayWithCapacity:0];
    _allCloseAry = [NSMutableArray arrayWithCapacity:0];
    _nameAry = @[@"一",@"二",@"三",@"四",@"五",@"六",@"日",];
    _closeType = 0;
    _page = 0;
    [self creatUI];
    [self requestWorkerPeopleList];
}

- (void)creatUI
{
    self.navigationItem.title = @"项目管理";
    self.view.backgroundColor = BacksColor;
    self.navigationController.navigationBar.translucent = NO;
    UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40*displaywidth)];
    topView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:topView];
    NSArray *titAry = @[@"签到",@"日报"];
    for (int i = 0; i < titAry.count; i++)
    {
        UIButton *botton = [ZCControl createButtonWithFrame:CGRectMake(20*displaywidth+(SCREEN_WIDTH/2)*i, 5*displaywidth, SCREEN_WIDTH/2-40*displaywidth, 30*displaywidth) ImageName:nil Target:self Action:@selector(closeTypeAction:) Title:titAry[i] Font:14.0];
        [botton setTitleColor:RGB(77, 77, 77) forState:UIControlStateNormal];
        botton.tag = 10+i;
        [topView addSubview:botton];
    }
    UIView *lineAview = [[UIView alloc] initWithFrame:CGRectMake(0, 39*displaywidth, SCREEN_WIDTH, 1*displaywidth)];
    lineAview.backgroundColor = LinesColor;
    [topView addSubview:lineAview];
    self.theLineView = [[UIView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH/4-20*displaywidth, 38*displaywidth, 40*displaywidth, 2*displaywidth)];
    _theLineView.backgroundColor = RGB(232, 80, 75);
    [topView addSubview:_theLineView];
    CGFloat height = SCREEN_HEIGHT-STA_NAV_HEIGHT-40*displaywidth-SCREER_HeiX;
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 40*displaywidth, SCREEN_WIDTH, height)];
    _scrollView.delegate = self;
    _scrollView.backgroundColor = BacksColor;
    _scrollView.contentOffset = CGPointMake(0, 0);
    _scrollView.pagingEnabled = NO;
    _scrollView.bounces = NO;
    _scrollView.showsHorizontalScrollIndicator = YES;
    _scrollView.contentSize = CGSizeMake(SCREEN_WIDTH*2, height);
    _scrollView.scrollEnabled = NO;
    [self.view addSubview:_scrollView];
    leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, height)];
    leftView.backgroundColor = BacksColor;
    [_scrollView addSubview:leftView];
    UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc]init];
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    _collectionView=[[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40*displaywidth*6) collectionViewLayout:layout];
    _collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.tag = 33;
    [leftView addSubview:_collectionView];
    [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:CellIdentifier];
    [self getNewMonthAllDay:[NSDate date]];
    
    self.tableview = [[UITableView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH, 0, SCREEN_WIDTH, height) style:UITableViewStylePlain];
    _tableview.delegate = self;
    _tableview.dataSource = self;
    _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableview.backgroundColor = BacksColor;
    [_scrollView addSubview:_tableview];
    self.tableview.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewHeaderData)];
    self.tableview.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreFootData)];
    self.tableview.mj_footer.automaticallyHidden = YES;
    self.tableview.estimatedRowHeight = 0;
    self.tableview.estimatedSectionHeaderHeight = 0;
    self.tableview.estimatedSectionFooterHeight = 0;
    UIButton *addBtn = [ZCControl createButtonWithFrame:CGRectMake(SCREEN_WIDTH*2-55*displaywidth, height-100*displaywidth, 40*displaywidth, 40*displaywidth) ImageName:@"xieribao" Target:self Action:@selector(addWorkerDaily) Title:nil Font:12.0];
    addBtn.backgroundColor = [UIColor clearColor];
    [_scrollView addSubview:addBtn];
    
}

- (void)loadNewHeaderData
{
    _page = 0;
    [self requestWorkerDailyList];
}

- (void)loadMoreFootData
{
    _page++;
    [self requestWorkerDailyList];
}

- (void)addWorkerDaily
{
    DailyViewController *vc = [[DailyViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)getNewMonthAllDay:(NSDate *)date
{
    NSUInteger firstDayOrdinalInWeek = [Helper firstDayOfMonthOrdinalNumberInWeek:date];
    NSUInteger days = [Helper totalDaysInMonth:date];
    NSUInteger todayOrdinalInMonth = [Helper todaysOrdinalInMonth:date];
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init]; //初始化格式器。
    [formatter setDateFormat:@"YYYY-MM-dd"];//定义时间为这种格式： YYYY-MM-dd hh:mm:ss 。
    NSString *currentTime = [formatter stringFromDate:date];
    NSArray *timeAry = [currentTime componentsSeparatedByString:@"-"];
    NSString *yearStr = timeAry[0];
    NSString *monthStr = timeAry[1];
    [_dayAry removeAllObjects];
    NSInteger numcount = days+firstDayOrdinalInWeek-2;
    if (firstDayOrdinalInWeek == 1)
    {
        numcount = days+6;
    }
    for (int i = 0; i < numcount; i++)
    {
        TimeModel *model = [[TimeModel alloc] init];
        model.year = yearStr;
        model.month = monthStr;
        NSInteger time = i-firstDayOrdinalInWeek+3;
        if (firstDayOrdinalInWeek == 1)
        {
            time = i-6+firstDayOrdinalInWeek;
        }
        model.day = [NSString stringWithFormat:@"%ld",time];
        model.subTime = [NSString stringWithFormat:@"%@-%@-%@",yearStr,monthStr,model.day];
        if (time > 0)
        {
            model.isHave = YES;
        }
        if (time == todayOrdinalInMonth && time > 0)
        {
            model.isToday = YES;
        }
        [_dayAry addObject:model];
    }
    NSInteger num = _dayAry.count/7;
    if (_dayAry.count%7 != 0)
    {
        num++;
    }
    num += 1;
    _collectionView.frame = CGRectMake(0, 0, SCREEN_WIDTH, num*40*displaywidth);
    [_collectionView reloadData];
    CGFloat Height = num*40*displaywidth;
    UILabel *titLab = [ZCControl createLabelWithFrame:CGRectMake(0, Height+5*displaywidth, SCREEN_WIDTH, 30*displaywidth) Font:14.0 Text:@"   考勤状况" Color:RGB(77, 77, 77)];
    titLab.backgroundColor = [UIColor whiteColor];
    [leftView addSubview:titLab];
    CGFloat H = SCREEN_HEIGHT-STA_NAV_HEIGHT-40*displaywidth-SCREER_HeiX;
    UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(10*displaywidth, Height + 40*displaywidth, SCREEN_WIDTH-20*displaywidth, H-Height-55*displaywidth)];
    bottomView.backgroundColor = [UIColor whiteColor];
    bottomView.layer.cornerRadius = 10;
    bottomView.layer.masksToBounds = YES;
    [leftView addSubview:bottomView];
    CGFloat W = SCREEN_WIDTH-20*displaywidth;
    NSArray *titArray = @[@" 正常",@" 迟到",@" 缺勤"];
    for (int i = 0; i < titArray.count; i++)
    {
        NSString *imgUrl = @"kuang";
        if (i == 0)
        {
            imgUrl = @"gou";
        }
        UIButton *button = [ZCControl createButtonWithFrame:CGRectMake(W/3*i, 5*displaywidth, W/3, 30*displaywidth) ImageName:imgUrl Target:self Action:@selector(closeStausAction:) Title:titArray[i] Font:12.0];
        button.tag = 50+i;
        [button setTitleColor:RGB(77, 77, 77) forState:UIControlStateNormal];
        [bottomView addSubview:button];
    }
    UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc]init];
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    self.nameCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 35*displaywidth, W, H-Height-145*displaywidth) collectionViewLayout:layout];
    _nameCollectionView.backgroundColor = [UIColor whiteColor];
    _nameCollectionView.delegate = self;
    _nameCollectionView.dataSource = self;
    _nameCollectionView.tag = 44;
    [bottomView addSubview:_nameCollectionView];
    [_nameCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:CellNamePath];
    
    UIButton *nextBtn = [ZCControl createButtonWithFrame:CGRectMake(60*displaywidth, H-Height-110*displaywidth, W-120*displaywidth, 45*displaywidth) ImageName:nil Target:self Action:@selector(nextAction) Title:@"下一步" Font:16.0];
    nextBtn.backgroundColor = ThemeColor;
    nextBtn.layer.cornerRadius = 5;
    nextBtn.layer.masksToBounds = YES;
    [nextBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [bottomView addSubview:nextBtn];
}


- (void)nextAction
{
    if (_allCloseAry.count == 0)
    {
        [LTools  showMBProgressWithText:@"请选择要考勤的工人" addToView:self.view];
        return;
    }
//    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"设置选中工人工作时长(/小时)" message:@"提示：设置之后将无法修改" preferredStyle:UIAlertControllerStyleAlert];
//    WEAKSELF;
//    // 添加按钮
//    __weak typeof(alert) weakAlert = alert;
//    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
//
//    }]];
//
//    [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
//        [weakSelf requestSaveAttendance:[weakAlert.textFields.firstObject text]];
//    }]];
//
//    // 添加文本框
//    [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
//        textField.textColor = RGB(77, 77, 77);
//        textField.textAlignment = NSTextAlignmentCenter;
//        textField.keyboardType = UIKeyboardTypeNumberPad;
//        [textField addTarget:self action:@selector(usernameDidChange:) forControlEvents:UIControlEventEditingChanged];
//
//    }];
    [self showTipView];
    
//    [self presentViewController:alert animated:YES completion:nil];
}

- (void)showTipView {
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(keyboardHide:)];
    singleTap.cancelsTouchesInView = NO;
    [window addGestureRecognizer:singleTap];
    if (!_coverView) {
        _coverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        _coverView.backgroundColor = [UIColor blackColor];
        _coverView.alpha = 0;
        [window addSubview:_coverView];
    }
    _coverView.hidden = NO;
    
    if (!_tipView) {
        _tipView = [[UIView alloc] initWithFrame:CGRectMake(40, (SCREEN_HEIGHT - 302*displaywidth)/2, SCREEN_WIDTH - 80, 302*displaywidth)];
        _tipView.backgroundColor = [UIColor whiteColor];
        _tipView.alpha = 0;
        _tipView.layer.cornerRadius = 10;
        _tipView.layer.masksToBounds = YES;
        [window addSubview:_tipView];
    }
    _tipView.hidden = NO;
    [UIView transitionWithView:_coverView duration:0.2 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
        _coverView.alpha = 0.4;
        _tipView.alpha = 1;
    } completion:nil];
    UILabel *tipLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 8, _tipView.frame.size.width, 30)];
    tipLabel.text = @"设置选中工人工作时长(/小时)";
    tipLabel.textAlignment = NSTextAlignmentCenter;
    tipLabel.font = [UIFont systemFontOfSize:18];
    tipLabel.textColor = UIColorHexValue(0x4D4D4D);
    [_tipView addSubview:tipLabel];
//    UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(18, 56, _tipView.frame.size.width - 36, 250*displaywidth)];
//    [webView loadHTMLString:agreementStr baseURL:nil];
//    webView.backgroundColor = [UIColor whiteColor];
//    [_tipView addSubview:webView];
    UILabel *lineLabel1 = [ZCControl createLabelWithFrame:CGRectMake(0, 46, _tipView.frame.size.width, 1) Font:16 Text:nil Color:nil];
    lineLabel1.backgroundColor = RGB(233, 234, 233);
    [_tipView addSubview:lineLabel1];
    UILabel *lineLabel = [ZCControl createLabelWithFrame:CGRectMake(0, _tipView.frame.size.height - 50, _tipView.frame.size.width, 1) Font:16 Text:nil Color:nil];
    lineLabel.backgroundColor = RGB(233, 234, 233);
    [_tipView addSubview:lineLabel];
    
    if (!selectPickerView) {
        selectPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0,46,_tipView.frame.size.width,216)];
        selectPickerView.showsSelectionIndicator = YES;
        selectPickerView.delegate = self;
        selectPickerView.dataSource = self;
        [_tipView addSubview:selectPickerView];
    }
    
    UILabel *dianLabel = [ZCControl createLabelWithFrame:CGRectMake(0, 0, 20, 20) Font:16 Text:@"." Color:UIColorHexValue(0xFF333333)];
    dianLabel.textAlignment = NSTextAlignmentCenter;
    dianLabel.center = CGPointMake(_tipView.frame.size.width/2, 150);
    [_tipView addSubview:dianLabel];
    
    UIButton *cancelBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, _tipView.frame.size.height - 40, _tipView.frame.size.width / 2, 30)];
    cancelBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [cancelBtn setTitleColor:UIColorHexValue(0xFF333333) forState:UIControlStateNormal];
    cancelBtn.backgroundColor = [UIColor clearColor];
    [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [cancelBtn addTarget:self action:@selector(onClickCancelBtn) forControlEvents:UIControlEventTouchUpInside];
    [_tipView addSubview:cancelBtn];
    UIButton *ensureBtn = [[UIButton alloc] initWithFrame:CGRectMake(_tipView.frame.size.width / 2, _tipView.frame.size.height - 40, _tipView.frame.size.width / 2, 30)];
    ensureBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    ensureBtn.backgroundColor = [UIColor clearColor];

    [ensureBtn setTitle:@"确定" forState:UIControlStateNormal];
    [ensureBtn setTitleColor:ThemeColor forState:UIControlStateNormal];
    [ensureBtn addTarget:self action:@selector(onClickEnsureBtn) forControlEvents:UIControlEventTouchUpInside];
    [_tipView addSubview:ensureBtn];
}

- (void)onClickCancelBtn {
    [self hideTipView];
}

- (void)onClickEnsureBtn {
    NSString *str;
    if (selectedMajor==0) {
        str = @"8.0";
    } else if (selectedMajor==majorNames.count-1) {
        str = @"0.5";
    } else {
        str = [NSString stringWithFormat:@"%@.%@", majorNames[selectedMajor], grades[selectedGrade]];
    }
    [self hideTipView];
    [self requestSaveAttendance:str];
}

-(void)keyboardHide:(UITapGestureRecognizer*)tap {
    [self hideTipView];
}

- (void)hideTipView {
    [selectPickerView reloadAllComponents];

    if (_coverView) {
        [UIView transitionWithView:_coverView duration:0.2 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
            _coverView.alpha = 0;
        } completion:nil];
        _coverView.hidden = YES;
    }
    if (_tipView) {
        [UIView transitionWithView:_tipView duration:0.2 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
            _tipView.alpha = 0;
        } completion:nil];
        _tipView.hidden = YES;
    }
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return componentCount;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    if (component==majorComponent) {
        return [majorNames count];
    } else {
        if (selectedMajor==0 || selectedMajor==majorNames.count-1) {
            return 1;
        } else {
            return [grades count];
        }
    }
}
//- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
//    UILabel *printString;
//    if (component==majorComponent) {
//        printString= [[UILabel alloc] initWithFrame:CGRectMake(5,0,majorComponentWidth,32)];
//        printString.text = [majorNames objectAtIndex:row];
//        [printString setFont:[UIFont fontWithName:@"Georgia" size:12.0f]];
//    } else {
//        printString= [[UILabel alloc] initWithFrame:CGRectMake(5,0,gradeComponentWidth,32)];
//        printString.text = [grades objectAtIndex:row];
//    }
//    printString.backgroundColor=[UIColor clearColor];
//    printString.textAlignment=NSTextAlignmentCenter;
//
//    return printString;
//}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    if (_closeType!=2) {
        if (component==majorComponent) {
            return [majorNames objectAtIndex:row];
        } else {
            if (selectedMajor==0) {
                return [grades objectAtIndex:1];
            } else if (selectedMajor==majorNames.count-1) {
                return [grades objectAtIndex:0];
            } else {
                return [grades objectAtIndex:row];
            }
        }
    } else {
        if (component==majorComponent) {
            return [majorNames objectAtIndex:row];
        } else {
            return [grades objectAtIndex:row];
        }
    }
//    return [NSString stringWithFormat:@"%@-%d", component == 1 ? @"R" : @"L", row];
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
    return 45.0;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
    return _tipView.frame.size.width/2 - 30;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    
    NSLog(@"Major [%ld] -- Grade [%ld]", [pickerView selectedRowInComponent:majorComponent], [pickerView selectedRowInComponent:gradeComponent]);
    selectedMajor = [pickerView selectedRowInComponent:majorComponent];
    selectedGrade = [pickerView selectedRowInComponent:gradeComponent];
    [selectPickerView reloadComponent:1];
}


- (void)usernameDidChange:(UITextField *)username
{
    NSLog(@"%@", username.text);
}
//考勤状态
- (void)closeStausAction:(UIButton *)btn
{
    _closeType = btn.tag-50;
    if (_closeType==2) {
        majorNames=[[NSArray alloc]initWithObjects:@"0",nil ];
        grades=[[NSArray alloc]initWithObjects:@"0",nil];
    } else {
        majorNames=[[NSArray alloc]initWithObjects:@"8", @"7", @"6", @"5",@"4",@"3",@"2",@"1",@"0",nil ];
        grades=[[NSArray alloc]initWithObjects:@"5",@"0",nil];
    }
    for (id v in btn.superview.subviews)
    {
        if ([v isKindOfClass:[UIButton class]])
        {
            UIButton *button = (UIButton *)v;
            if (button.tag >= 50 && button.tag < 53)
            {
                [button setImage:[UIImage imageNamed:@"kuang"] forState:UIControlStateNormal];
            }
        }
    }
    [btn setImage:[UIImage imageNamed:@"gou"] forState:UIControlStateNormal];

}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dailyAry.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellPath];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (_dailyAry.count > 0)
    {
        projectModel *model = _dailyAry[indexPath.row];
        UILabel *projectNameLab = [ZCControl createLabelWithFrame:CGRectMake(15*displaywidth, 5*displaywidth, SCREEN_WIDTH-165*displaywidth, 25*displaywidth) Font:16.0 Text:model.projectname Color:ThemeColor];
        [cell.contentView addSubview:projectNameLab];
        NSString *timeStr = model.createDtm;
        if (timeStr.length > 15)
        {
            timeStr = [timeStr substringToIndex:model.createDtm.length-3];
        }
        
        UILabel *timeLab = [ZCControl createLabelWithFrame:CGRectMake(SCREEN_WIDTH-180*displaywidth, 5*displaywidth, 165*displaywidth, 25*displaywidth) Font:12.0 Text:timeStr Color:RGB(153, 153, 153)];
        timeLab.textAlignment = NSTextAlignmentRight;
        [cell.contentView addSubview:timeLab];
        
        
        UILabel *nameLab = [ZCControl createLabelWithFrame:CGRectMake(15*displaywidth, 30*displaywidth, SCREEN_WIDTH-30*displaywidth, 25*displaywidth) Font:13.0 Text:[NSString stringWithFormat:@"汇报人员：%@",model.user.name] Color:RGB(77, 77, 77)];
        [cell.contentView addSubview:nameLab];
        
        UILabel *contenLab = [ZCControl createLabelWithFrame:CGRectMake(15*displaywidth, 55*displaywidth, SCREEN_WIDTH-75*displaywidth, 25*displaywidth) Font:13.0 Text:[NSString stringWithFormat:@"汇报内容：%@",model.content] Color:RGB(77, 77, 77)];
        contenLab.lineBreakMode = NSLineBreakByTruncatingTail;
        contenLab.adjustsFontSizeToFitWidth = NO;
        [cell.contentView addSubview:contenLab];
        UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 84*displaywidth, SCREEN_WIDTH, 1*displaywidth)];
        lineView.backgroundColor = LinesColor;
        [cell.contentView addSubview:lineView];
        
    }
   
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    projectModel *model = _dailyAry[indexPath.row];
    DailyDetailsViewController *vc = [[DailyDetailsViewController alloc] init];
    vc.projectModel = model;
    [self.navigationController pushViewController:vc animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 85*displaywidth;
}


-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    if (collectionView.tag == 33)
    {
        return 2;
    }
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (collectionView.tag == 33)
    {
        if (section == 0)
        {
            return _nameAry.count;
        }
        return _dayAry.count;
    }
    return _allWorkerArray.count+1;
    
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *path = CellNamePath;
    if (collectionView.tag == 33)
    {
        path = CellIdentifier;
    }
    UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:path forIndexPath:indexPath];
    //    [cell.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    for (id subView in cell.contentView.subviews) {
        [subView removeFromSuperview];
    }
    if (collectionView.tag == 33)
    {
        CGFloat W = SCREEN_WIDTH/7;
        cell.backgroundColor = [UIColor whiteColor];
        if (indexPath.section == 0)
        {
            UILabel *titLab = [ZCControl createLabelWithFrame:CGRectMake(0, 10*displaywidth, W, 30*displaywidth) Font:14.0 Text:_nameAry[indexPath.row] Color:RGB(77, 77, 77)];
            titLab.textAlignment = NSTextAlignmentCenter;
            [cell.contentView addSubview:titLab];
            
        }
        else
        {
            if (_dayAry.count > 0)
            {
                TimeModel *model = _dayAry[indexPath.row];
                if ([model.day integerValue] > 0)
                {
                    UILabel *dayLab = [ZCControl createLabelWithFrame:CGRectMake((W-30*displaywidth)/2, 5*displaywidth, 30*displaywidth, 30*displaywidth) Font:14.0 Text:model.day Color:RGB(77, 77, 77)];
                    dayLab.textAlignment = NSTextAlignmentCenter;
                    if (model.isToday)
                    {
                        dayLab.layer.cornerRadius = 15*displaywidth;
                        dayLab.layer.masksToBounds = YES;
                        dayLab.textColor = [UIColor whiteColor];
                        dayLab.backgroundColor = RGB(228, 80, 76);
                    }
                    [cell.contentView addSubview:dayLab];
                }
            }
        }
    }
    else
    {
        CGFloat W = (SCREEN_WIDTH-20*displaywidth)/5;
        if (indexPath.row == 0)
        {
            NSString *nameString = @"全选";
            if (_isQX)
            {
                nameString = @"取消";
            }
            UILabel *nameLab = [ZCControl createLabelWithFrame:CGRectMake(0, 5*displaywidth, W, 45*displaywidth) Font:14.0 Text:nameString Color:ThemeColor];
            nameLab.textAlignment = NSTextAlignmentCenter;
            [cell.contentView addSubview:nameLab];
        }
        else
        {
            werkerModel *model = _allWorkerArray[indexPath.row-1];
            NSString *name = model.linkman;
            NSString *nameStr = @"";
            if (name.length > 0)
            {
                nameStr = [name substringToIndex:1];
            }
            UILabel *titLab = [ZCControl createLabelWithFrame:CGRectMake(W/2-15*displaywidth, 5*displaywidth, 30*displaywidth, 30*displaywidth) Font:20.0 Text:nameStr Color:[UIColor whiteColor]];
            titLab.textAlignment = NSTextAlignmentCenter;
            titLab.backgroundColor = RGB(236, 165, 84);
            titLab.layer.cornerRadius = 15*displaywidth;
            titLab.layer.masksToBounds = YES;
            [cell.contentView addSubview:titLab];
            if ([model.status intValue] != 0)
            {
                titLab.backgroundColor = RGBA(0, 0, 0, 0.6);
            }
            else
            {
                if ([_allCloseAry containsObject:[NSString stringWithFormat:@"%ld",indexPath.row]])
                {
                    UIImageView *gouxuanImgview = [ZCControl createImageViewWithFrame:CGRectMake(0, 0, 30*displaywidth, 30*displaywidth) ImageName:@"heisegou"];
                    [titLab addSubview:gouxuanImgview];
                }
            }
            UILabel *nameLabel = [ZCControl createLabelWithFrame:CGRectMake(0, 35*displaywidth, W, 25*displaywidth) Font:12.0 Text:name Color:RGB(77, 77, 77)];
            nameLabel.textAlignment = NSTextAlignmentCenter;
            [cell.contentView addSubview:nameLabel];
            
        }
    }
    
    
    return cell;
}

//定义每一个cell的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (collectionView.tag == 33)
    {
        return CGSizeMake(SCREEN_WIDTH/7, 40*displaywidth);

    }
    return CGSizeMake((SCREEN_WIDTH-20*displaywidth)/5, 60*displaywidth);

}

//cell的点击事件
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    //cell被电击后移动的动画
    [collectionView selectItemAtIndexPath:indexPath animated:YES scrollPosition:UICollectionViewScrollPositionTop];
    if (collectionView.tag == 44)
    {
        if (indexPath.row == 0)
        {
            _isQX = !_isQX;
            [_allCloseAry removeAllObjects];
            if (_isQX)
            {
                for (int i = 0; i < _allWorkerArray.count; i++)
                {
                    werkerModel *model = _allWorkerArray[i];
                    if ([model.status intValue] == 0)
                    {
                        [_allCloseAry addObject:[NSString stringWithFormat:@"%d",i+1]];
                    }
                }
            }
            
            [collectionView reloadData];
        }
        else
        {
            werkerModel *model = _allWorkerArray[indexPath.row-1];
            if ([model.status intValue] != 0)
            {
                return;
            }
            if ([_allCloseAry containsObject:[NSString stringWithFormat:@"%ld",indexPath.row]])
            {
                [_allCloseAry removeObject:[NSString stringWithFormat:@"%ld",indexPath.row]];
            }
            else
            {
                [_allCloseAry addObject:[NSString stringWithFormat:@"%ld",indexPath.row]];
            }
            [UIView performWithoutAnimation:^{
                [collectionView reloadItemsAtIndexPaths:@[indexPath]];
            }];
        }
        
    }
}


- (void)closeTypeAction:(UIButton *)btn
{
    if (btn.tag == 10)
    {
        [_scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
        _theLineView.frame = CGRectMake(SCREEN_WIDTH/4-20*displaywidth, 38*displaywidth, 40*displaywidth, 2*displaywidth);
    }
    else
    {
        [_scrollView setContentOffset:CGPointMake(SCREEN_WIDTH, 0) animated:YES];
        _theLineView.frame = CGRectMake(SCREEN_WIDTH/4*3-20*displaywidth, 38*displaywidth, 40*displaywidth, 2*displaywidth);
    }
}


- (void)requestWorkerDailyList
{
    WEAKSELF;
    NSDictionary *dic = @{@"page":[NSString stringWithFormat:@"%ld",_page],
                          @"project.id":[LTools cacheForKey:SERVER_ManageID]
                          };
    [HYBaseHandler requestWorkerDailyParmers:dic Success:^(id obj) {
        [weakSelf.tableview.mj_header endRefreshing];
        [weakSelf.tableview.mj_footer endRefreshing];
        if ([obj server_Getcodecheck])
        {
            NSArray *dailyArray = [projectModel mj_objectArrayWithKeyValuesArray:obj[@"data"][@"content"]];
            if (_page == 0)
            {
                [weakSelf.dailyAry removeAllObjects];
            }
            [weakSelf.dailyAry addObjectsFromArray:dailyArray];
            
        }
        else
        {
            [LTools showMBProgressWithText:obj[@"msg"] addToView:weakSelf.view];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [weakSelf.tableview reloadData];
        });
    } failed:^(NSError *error) {
        [weakSelf.tableview.mj_header endRefreshing];
        [weakSelf.tableview.mj_footer endRefreshing];
    }];
}

//员工列表
- (void)requestWorkerPeopleList
{
    WEAKSELF;
    NSString *str = [LTools cacheForKey:SERVER_manageID];
    NSDictionary *dic = @{@"projectId":[NSString stringWithFormat:@"%@",str]};
    [HYBaseHandler requestWorkerPeopleListParmers:dic Success:^(id obj) {
        if ([obj server_Getcodecheck])
        {
            _allWorkerArray = [werkerModel mj_objectArrayWithKeyValuesArray:obj[@"data"]];
            dispatch_async(dispatch_get_main_queue(), ^{
                [weakSelf.nameCollectionView reloadData];
            });
        }
        else
        {
            [LTools showMBProgressWithText:obj[@"msg"] addToView:weakSelf.view];
        }
    } failed:^(NSError *error) {
        
    }];
}

- (void)requestSaveAttendance:(NSString *)hours
{
    if ([hours floatValue] == 0)
    {
        [LTools showMBProgressWithText:@"请设置工作时长" addToView:self.view];
//        [self nextAction];
        return;
    }
    if ([hours integerValue] > 24)
    {
        [LTools showMBProgressWithText:@"不能超过24小时" addToView:self.view];
        return;
    }
    WEAKSELF;
    NSMutableArray *bigAry = [NSMutableArray arrayWithCapacity:0];
    for (NSString *index in _allCloseAry)
    {
        werkerModel *model = _allWorkerArray[[index integerValue]-1];
        NSDictionary *dic = @{@"projectId":[LTools cacheForKey:SERVER_manageID],
                              @"workerId":[NSString stringWithFormat:@"%@",model.workerId],
                              @"hours":hours,
                              @"status":[NSString stringWithFormat:@"%ld",_closeType+1]
                              };
        [bigAry addObject:dic];
    }
    [HYBaseHandler requestSaveAttendanceParmers:bigAry Success:^(id obj) {
        if ([obj server_Getcodecheck])
        {
            [weakSelf.allCloseAry removeAllObjects];
            [LTools showMBProgressWithText:@"设置成功" addToView:weakSelf.view];
            
            [weakSelf requestWorkerPeopleList];
        }
        else
        {
            [LTools showMBProgressWithText:obj[@"msg"] addToView:weakSelf.view];
        }
    } failed:^(NSError *error) {
        
    }];
    selectedGrade = 0;
    selectedMajor = 0;
    
    [self hideTipView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
